package com.eastfair.point.transaction;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.mq.model.MySimpleMessage;
import com.eastfair.point.PointKeyDefinition;
import com.eastfair.point.point.CardTransaction;
import com.eastfair.point.point.OrderTransaction;
import com.eastfair.point.point.StoreTransaction;
import com.eastfair.point.service.CardService;
import com.eastfair.point.service.OrderService;
import com.eastfair.point.service.StoreService;
import com.eastfair.transaction.util.TransactionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 一个项目有且仅有一个@RocketMQTransactionListener注解
 * @author: ligang
 * @date: 2022年05月01日 17:26
 */
@Slf4j
@RocketMQTransactionListener
public class GlobalTransactionListner implements RocketMQLocalTransactionListener {

    @Autowired
    private OrderService orderService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private CardService cardService;

    @Autowired
    TransactionUtil transactionUtil;

    /**
     * 执行本地事务
     */
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        try {
            //执行本地事务
            MySimpleMessage mySimpleMessage = JSONObject.parseObject(new String((byte[]) msg.getPayload()), MySimpleMessage.class);
            //获取发送方
            String sender = transactionUtil.querySender(mySimpleMessage);
            if (OrderTransaction.class.getName().equals(sender)) {
                OrderTransaction orderTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(), OrderTransaction.class);
                return orderService.executeCommit(orderTransaction);
            } else if (StoreTransaction.class.getName().equals(sender)) {
                StoreTransaction storeTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(), StoreTransaction.class);
                return storeService.executeCommit(storeTransaction);
            } else if (CardTransaction.class.getName().equals(sender)) {
                CardTransaction cardTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(), CardTransaction.class);
                return cardService.executeCommit(cardTransaction);
            }
            return RocketMQLocalTransactionState.COMMIT;
        } catch (Exception e) {
            e.printStackTrace();
            return RocketMQLocalTransactionState.ROLLBACK;
        }
    }

    /**
     * 本地事务的检查，检查本地事务是否成功
     */
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
        MySimpleMessage mySimpleMessage = JSONObject.parseObject(new String((byte[]) msg.getPayload()), MySimpleMessage.class);
        //获取发送方
        String sender = transactionUtil.querySender(mySimpleMessage);
        if (PointKeyDefinition.TRANSACTION_ORDER.equals(sender)) {
            OrderTransaction orderTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(), OrderTransaction.class);
            return orderService.backTransaction(orderTransaction);
        } else if (PointKeyDefinition.TRANSACTION_STORE.equals(sender)) {
            StoreTransaction storeTransaction = JSONObject.parseObject(mySimpleMessage.getPayload().toString(), StoreTransaction.class);
            return storeService.backTransaction(storeTransaction);
        }
        return RocketMQLocalTransactionState.COMMIT;
    }

}
